package com.example.weddingapp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.lifecycle.AndroidViewModel;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.weddingapp.R;
import com.example.weddingapp.base.BaseActivity;
import com.example.weddingapp.databinding.ActivityAddInfoBinding;
import com.example.weddingapp.utils.CommonUtil;

import java.util.Date;

/**
 * @Date 2020/5/9 11:31
 * @Desc 填写信息
 */
public class AddInfoActivity extends BaseActivity<AndroidViewModel, ActivityAddInfoBinding> {
    private Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_info);

        showContentView();

    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("活动信息");
        showRightButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    /**
     * 提交
     */
    private void submit() {
        //新郎
        String groomName = bindingView.etGroomName.getText().toString().trim();
        //新娘
        String brideName = bindingView.etBrideName.getText().toString().trim();
        //时间
        String time = bindingView.tvTime.getText().toString().trim();
        //地址
        String address = bindingView.etAddress.getText().toString().trim();

        if (TextUtils.isEmpty(groomName)) {
            showToast("请输入新郎姓名");
            return;
        }

        if (TextUtils.isEmpty(brideName)) {
            showToast("请输入新娘姓名");
            return;
        }
        if (TextUtils.isEmpty(time)) {
            showToast("请选择日期");
            return;
        }
        if (TextUtils.isEmpty(address)) {
            showToast("请输入地址");
            return;
        }

        Intent intent = new Intent(this, AddActivity.class);
        intent.putExtra("address", address);
        intent.putExtra("time", time);
        intent.putExtra("brideName", brideName);
        intent.putExtra("groomName", groomName);
        intent.putExtra("timeDate",AddInfoActivity.this.date);
        startActivity(intent);
        finish();
    }

    @Override
    protected void initData() {
        super.initData();
        bindingView.tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectTime();
            }
        });
    }

    /**
     * 选择时间
     */
    private void selectTime() {
        TimePickerView dateBtn = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                AddInfoActivity.this.date = date;
                String time = CommonUtil.formatDate(date, "yyyy年MM月dd日 HH时mm分");
                bindingView.tvTime.setText(time);
            }
        })
                .setTitleBgColor(Color.RED)
                .setCancelColor(Color.WHITE)
                .setSubmitColor(Color.WHITE)
                .setType(new boolean[]{true, true, true, true, true, false})
                .setDecorView((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content))
                .setLabel("", "", "", "", "", "")
                .build();
        dateBtn.show();
    }

}
